//Common.js

//================================================弹出层脚本==================================================
//使用说明：

//方法参数：
//ShowBox(conf, confirmCallBack, cancelCallBack, closeCallBack)
//conf：配置对象；
//confirmCallBack：确认回调函数；
//cancelCallBack：取消回调函数；
//closeCallBack：关闭回调函数；

//外部调用关闭方法：
//ShowBox.prototype.Close

////配置对象
//var config = {
//    Type: "information",//类型：common：普通；information：提示；confirm：确认；
//    Title: "提示",
//    Content: "",//内容
//    Width: 400,//宽度
//    Height: 100,//高度
//    FadeIn: 170,//打开速度（毫秒）
//    FadeOut: 170,//关闭速度（毫秒）
//    ShowScroll: false//是否显示滚动条
//};

//HTML代码：
//<div class="blacklayer">
//    </div>
//    <div class="poplayer">
//        <div class="tit">
//            <span><!--标题文字--></span>
//            <div class="close">X</div>
//        </div>
//        <div class="con">
//            <div class="message">
//                <!--内容区-->
//            </div>
//            <div class="dbutton">
//                <a id="cancel" class="btn" href="javascript:;">取消</a>&nbsp;&nbsp;&nbsp;&nbsp;
//                <a id="confirm" class="btn" href="javascript:;">确定</a>
//            </div>
//        </div>
//    </div>

//可移动弹出层代码
function ShowBox(conf, confirmCallBack, cancelCallBack, closeCallBack) {
    //配置信息及合并
    var config = {
        Type: "information",//类型：common：普通；information：提示；confirm：确认；
        Title: "提示消息",
        Content: "",//内容
        Width: 400,//宽度
        Height: 100,//高度
        FadeIn: 170,//打开速度（毫秒）
        FadeOut: 170,//关闭速度（毫秒）
        ShowScroll: true//是否显示滚动条
    };
    $.extend(config, conf);
    switch (config.Type.toLowerCase()) {
        case "common":
            {
                $(".tit span").text(config.Title);
                $(".poplayer .message").html(config.Content);
                //$("#cancel").css("display", "block");
                $("#cancel,#confirm").css("textAlign", "center");
                break;
            }
            //case "information":
            //    {
            //        $(".tit span").text(config.Title);
            //        $(".poplayer .message").css("textAlign", "center");
            //        $(".poplayer .message").css("lineHeight", "50px");
            //        $("#cancel,#confirm").css("textAlign", "center");
            //        $(".poplayer .message").text(config.Content);
            //        $("#cancel").css("display", "none");
            //        break;
            //    }
        case "information":
            {
                $(".tit span").text(config.Title);
                $(".poplayer .message").css("textAlign", "center");
                $(".poplayer .message").css("lineHeight", "50px");
                //$("#cancel").css("display", "none");
                $("#cancel,#confirm").css("textAlign", "center");
                $(".poplayer .message").text(config.Content);
                break;
            }
        case "confirm":
            {
                $(".tit span").text(config.Title);
                $(".poplayer .message").css("textAlign", "center");
                $(".poplayer .message").css("lineHeight", "50px");
                //$("#cancel").css("display", "block");
                $("#cancel,#confirm").css("textAlign", "center");
                $(".poplayer .message").text(config.Content);
                break;
            }
    }

    //设置样式及位置
    $(".blacklayer").height($(document).height());
    if (!config.ShowScroll)
        $("body").css("overflow", "hidden");
    $(".blacklayer,.poplayer").fadeIn(config.FadeIn);
    $(".poplayer").width(config.Width);
    $(".poplayer").height(config.Height + 100);
    $(".con,.message").width(config.Width);
    $(".con").height(config.Height + 100);
    $(".message").height(config.Height);
    var leftOffset = ($(window).width() - $(".poplayer").width()) / 2;
    var topOffset = ($(window).height() - $(".poplayer").height()) / 2;
    $(".poplayer").css("left", leftOffset);
    if (config.Type.toLowerCase() === "confirm" || config.Type.toLowerCase() === "information")
        topOffset -= 150;
    $(".poplayer").css("top", topOffset);

    //移动事件
    var minuX = 0;
    var minuY = 0;
    var mouseDown = false;
    $(".tit").unbind("mousedown").bind("mousedown", function (e) {
        minuX = e.pageX - parseInt($(".poplayer").css("left"));
        minuY = e.pageY - parseInt($(".poplayer").css("top"));
        $(document).unbind("mousemove").bind("mousemove", function (e) {
            $(".poplayer").css("left", (e.pageX - minuX) + "px")
            .css("top", (e.pageY - minuY) + "px");
        });
    });
    $(document).unbind("mouseup")
             .unbind("mouseleave")
             .bind("mouseup", function () {
                 $(document)
                     .unbind("mousemove");
             })
             .bind("mouseleave", function () {
                 $(document).unbind("mousemove");
             });
    function closeForm() {
        //$("body").css("overflow-x", "hidden");
        $("body").css("overflow-y", "scroll");
        $(".blacklayer,.poplayer").fadeOut(config.FadeOut);
        config = null;
    }
    //确认回调
    $("#confirm").unbind("click").click(function () {
        closeForm();
        if (typeof (confirmCallBack) === "function") {
            confirmCallBack();
            closeForm();
        }
    });
    //取消回调
    $("#cancel").unbind("click").click(function () {
        closeForm();
        if (typeof (cancelCallBack) === "function") {
            cancelCallBack();
            closeForm();
        }
    });
    //关闭回调
    $(".close").unbind("click").click(function () {
        closeForm();
        if (typeof (closeCallBack) === "function") {
            closeCallBack();
            closeForm();
            //return false;
        }
    });
}
//外部调用关闭弹窗
ShowBox.prototype.Close = function () {
    closeForm();
}

//==============================================================================================================

//===============================================分页页码显示插件===============================================
//方法参数：
//Pager(conf, beforeSendFunction, completeFunction, successFunction, errorFunction)
//conf：配置对象，配置config内的参数；
//beforeSendFunction：发送请求前；
//completeFunction：完成请求；
//successFunction：请求成功；
//errorFunction：请求失败；

//AJAX分页扩展方法
function Pager(conf, successFunction, errorFunction, beforeSendFunction, completeFunction) {
    var config = {
        url: "",//请求URL
        type: "post",//请求方式
        async: true,//是否异步
        totalCount: 200,//总记录数，请求结果中赋值
        pageSize: 10,//页大小
        displayCount: 7,//为奇数
        div: "#pages",//分页插件放置的DIV
        dataType: "JSON",
        page: 1//默认请求页
    };
    $.extend(config, conf);
    $.ajax({
        url: config.url,
        type: config.type,
        cache: false,
        data: config,
        dataType: config.dataType,
        beforeSend: function () {
            if (typeof (beforeSendFunction) == "function")
                beforeSendFunction();
        },
        success: function (result) {

            config.totalCount = result.TotalCount;
            var pageCount = parseInt(config.pageSize > 0 ? (config.totalCount + config.pageSize - 1) / config.pageSize : 0);//页数
            if (config.page < 1 || config.page > pageCount)
                config.page = 1;
            var minMiddleNumber = Math.ceil(config.displayCount / 2);//最小中间号
            var maxMiddleNumber = Math.ceil((2 * pageCount - config.displayCount + 1) / 2);//最大中间号
            var pagerString = "";
            var pagerList = "";
            var objValues = "{url:'" + config.url + "',type:'" + config.type + "',async:" + config.async + ",page:{1}, pageSize:" + config.pageSize + ", displayCount:" + config.displayCount + ", div:'" + config.div + "', totalCount:" + config.totalCount + "}";
            var listStr = "<li><a {0} href=\"javascript:;\" onclick=\"Pager(" + objValues + "," + beforeSendFunction + ", " + completeFunction + ", " + successFunction + ", " + errorFunction + ");\">{2}</a></li>";

            var pagerWidth = (config.displayCount + 4) * 50;//分页插件宽度
            //CSS样式

            pagerString += "<style type=\"text/css\">";
            pagerString += "#pager {";//DIV样式
            pagerString += "width: " + pagerWidth + "px;";
            pagerString += "height: 40px;";
            pagerString += "margin: 0px auto;";
            pagerString += "font-family: 'Microsoft YaHei'";
            pagerString += "}";

            pagerString += "#pager-ul {";//UL样式
            pagerString += "list-style: none;";
            pagerString += "padding: 0px;";
            pagerString += "margin: 0px;";
            pagerString += "width: " + pagerWidth + "px;";
            pagerString += "height: 40px;";
            pagerString += "float: left;";
            pagerString += "}";

            pagerString += "#pager-ul li {";//LI样式
            pagerString += "width: 40px;";
            pagerString += "height: 40px;";
            pagerString += "margin: 0px;";
            pagerString += "padding: 0px;";
            pagerString += "margin-right: 10px;";
            pagerString += "background-color: white;";
            pagerString += "text-align: center;";
            pagerString += "list-style: none;";
            pagerString += "float: left;";
            pagerString += "border: 0px;";
            pagerString += "}";

            pagerString += "#pager-ul li a {";//A样式
            pagerString += "border: solid 1px #CCC;";
            pagerString += "width: 38px;";
            pagerString += "height: 38px;";
            pagerString += "cursor: pointer;";
            pagerString += "line-height: 38px;";
            pagerString += "display: block;";
            pagerString += "color: black;";
            pagerString += "text-decoration: none;";
            pagerString += "}";
            pagerString += "</style>";

            pagerString += "<div id=\"pager\">";
            var activePageStyle = "style=\"background-color:#FF9900; border:solid 1px #FF9900; color:white;\"";
            if (result.TotalCount != null && result.TotalCount > 0) {
                var nullLink = "<li><a href=\"javascript:;\" style=\"cursor: default;\">{0}</a></li>";//空链接
                pagerString += "<ul id=\"pager-ul\">";
                if (config.page > 1) {
                    pagerString += String.format(listStr, "", 1, "<<");//首页
                    pagerString += String.format(listStr, "", config.page - 1, "<");//上一页
                }
                else {
                    pagerString += String.format(nullLink, "<<");//首页空链接
                    pagerString += String.format(nullLink, "<");//上一页空链接
                }
                if (pageCount > config.displayCount) {
                    if (config.page <= minMiddleNumber) {
                        //最小左侧及最小中间
                        for (var i = 1; i <= config.displayCount; i++)
                            pagerList += String.format(listStr, i == config.page ? activePageStyle : "", i, i);//LI标签
                    }
                    else {
                        if (config.page <= maxMiddleNumber) {
                            //中间
                            for (var i = config.page - Math.floor(config.displayCount / 2) ; i <= config.page + Math.floor(config.displayCount / 2) ; i++)
                                pagerList += String.format(listStr, i == config.page ? activePageStyle : "", i, i);//LI标签
                        }
                        else {
                            //右侧
                            for (var i = pageCount - config.displayCount + 1 ; i <= pageCount; i++)
                                pagerList += String.format(listStr, i == config.page ? activePageStyle : "", i, i);//LI标签
                        }
                    }
                }
                else {
                    //页数小于等于显示数，显示所有页标签
                    for (var i = 1; i <= pageCount; i++)
                        pagerList += String.format(listStr, i == config.page ? activePageStyle : "", i, i);//LI标签
                }
                pagerString += pagerList;
                if (config.page < pageCount) {
                    pagerString += String.format(listStr, "", config.page + 1, ">");//下一页
                    pagerString += String.format(listStr, "", pageCount, ">>");//尾页
                }
                else {
                    pagerString += String.format(nullLink, ">");//下一页空链接
                    pagerString += String.format(nullLink, ">>");//尾页空链接
                }
                pagerString += "</ul>";
            }
            else {
                pagerString += "<span style=\"color:red; width:500px; height:40px; font-size:12pt; line-height:40px;font-family: 'Microsoft YaHei'\">暂无数据</span>";
            }
            pagerString += "</div>";
            $(config.div).html(pagerString);
            if (typeof (successFunction) == "function")
                successFunction(result);
        },
        error: function (result) {
            if (typeof (errorFunction) == "function")
                errorFunction(result);
        },
        complete: function () {
            if (typeof (completeFunction) == "function")
                completeFunction();
        }
    });
}
//String 拼接字符串扩展方法
String.format = function () {
    if (arguments.length == 0)
        return null;
    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
};

//=============================================================================================================

